package pl.edu.agh.broker.mns.server;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import pl.edu.agh.broker.mns.server.internal.SimpleMultiNameService;
import pl.edu.agh.broker.mns.server.internal.SingleReferenceServer;
import pl.edu.agh.broker.mns.shared.MultiNameService;


public class MultiNameServiceStarter {

	public static MultiNameService createMultiNameService(
			String referenceSavePath) throws FileNotFoundException, IOException {
		SimpleMultiNameService simpleMultiNameService = new SimpleMultiNameService();
		simpleMultiNameService.start();
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
				referenceSavePath));
		oos.writeObject(UnicastRemoteObject.toStub(simpleMultiNameService));
		oos.close();
		return simpleMultiNameService;
	}

	public static MultiNameService createMultiNameService(int port)
			throws RemoteException {
		SingleReferenceServer srs = new SingleReferenceServer(port);
		SimpleMultiNameService smns = new SimpleMultiNameService(srs);
		srs.setReference(UnicastRemoteObject.toStub(smns));
		smns.start();
		return smns;
	}
}
